package com.boxman

import android.annotation.SuppressLint
import android.content.res.AssetManager
import android.graphics.BitmapFactory

import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntSize

// 箱子
open class Box: BaseGameObject {
    private var mBack: BaseGameObject?          // 背后对象
    protected var mCurrentImage: ImageBitmap    // 当前图片
    protected lateinit var mImage2: ImageBitmap // 图片2

    /**
     * 构造方法。
     * @param assetManager  资产管理者
     * @param back          背后对象
     * @param type          类型
     * @param x, y          左上角坐标
     */
    @SuppressLint("DefaultLocale")
    constructor(assetManager: AssetManager, back: BaseGameObject, type: Type, x: Int, y: Int):
        super(assetManager, type, x, y) {

        // 初始化成员
        mBack = back
        mCurrentImage = mImage
        try {
            val imageFilePath = String.format(Const.IMAGE_FILE_PATH, type.ordinal * 10 + 1)
            val inputStream = assetManager.open(imageFilePath)
            mImage2 = BitmapFactory.decodeStream(inputStream).asImageBitmap()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 绘制自身。
     * @param drawer    绘图者
     */
    override fun draw(drawer: DrawScope) {
        drawer.drawImage(
            image = mCurrentImage,
            srcSize = IntSize(mCurrentImage.width, mCurrentImage.height),
            dstOffset = IntOffset(mX * sSize, mY * sSize),
            dstSize = IntSize(sSize, sSize)
        )
    }

    /**
     * 移动。
     * @param direction     方向
     * @param moveResult    移动结果
     * @return 被推动的箱子
     */
    override fun move(direction: Int, moveResult: MoveResult): BaseGameObject? {
        var leftFromDest = false    // 是否离开目的地
        var destX = mX
        var destY = mY              // 新位置的坐标

        // 计算新位置
        when (direction) {
            Const.DIRECTION_LEFT -> destX--
            Const.DIRECTION_RIGHT -> destX++
            Const.DIRECTION_UP -> destY--
            Const.DIRECTION_DOWN -> destY++
        }

        // 如果旁边是墙或箱子，则直接返回
        if (Game.sBaseGameObjects[destY][destX]?.getType() == Type.WALL ||
            Game.sBaseGameObjects[destY][destX]?.getType() == Type.BOX
        ) {
            moveResult.result = MoveResult.Result.NO_MOVE
            return null
        }

        // 设置移动结果
        if (moveResult.result.ordinal < MoveResult.Result.NORMAL_MOVED.ordinal) {
            moveResult.result = MoveResult.Result.NORMAL_MOVED
        }

        // 判断是否要离开目的地
        if (mType == Type.BOX && mBack?.getType() == Type.DESTINATION) {
            leftFromDest = true
            moveResult.result = MoveResult.Result.LEFT_FROM_DEST
            mCurrentImage = mImage
        }

        // 用背后对象擦除自己
        Game.sBaseGameObjects[mY][mX] = mBack

        // 计算新位置
        mX = destX
        mY = destY

        // 设置新的背后对象
        mBack = Game.sBaseGameObjects[mY][mX]

        // 判断是否到达目的地
        if (mType == Type.BOX && mBack?.getType() == Type.DESTINATION) {
            if (leftFromDest) {
                moveResult.result = MoveResult.Result.NORMAL_MOVED
            } else {
                moveResult.result = MoveResult.Result.ARRIVED_ON_DEST
            }
            mCurrentImage = mImage2
        }

        // 将自己保存到数组中
        Game.sBaseGameObjects[mY][mX] = this

        return this
    }
}
